package com.space.school.services.impl;

import com.space.school.dao.UserDao;
import com.space.school.dao.impl.UserDaoImpl;
import com.space.school.domain.User;
import com.space.school.services.UserService;
import com.space.school.utils.PageBean;

import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

//    @Override
//    public List<User> findAll() {
//        return userDao.findAll();
//    }

    @Override
    public PageBean<User> search(int currentPage, int pageSize,Map<String, String[]> map) {
        int start = (currentPage - 1) * pageSize;
        List<User> list = userDao.search(start, pageSize,map);
        PageBean<User> pb = new PageBean<>();
        pb.setList(list);
        //        总数居
        int count = userDao.count(map);
        pb.setTotalCount(count);
//        每页数据数
        pb.setRows(pageSize);
//        当前页
        pb.setCurrentPage(currentPage);
//        总页数
//        int totalPage = (int) Math.ceil((double) list.size() / pageSize);
        int totalPage = (count%pageSize==0? count/pageSize: count/pageSize+1);
        pb.setTotalPage(totalPage);
        return pb;
    }

    @Override
    public int delete(int id) {
        return userDao.delete(id);
    }

    @Override
    public void update(User user) {
        userDao.update(user);
    }

    @Override
    public User findById(int id) {
        return userDao.findById(id);
    }

    @Override
    public void updateStatus(int id, String status) {
        if (status.equals("1")) {
            status = "0";
        } else {
            status = "1";
        }
        userDao.updateStatus(id, status);
    }

    @Override
    public void save(User user) {
        userDao.save(user);
    }

}
